package com.zyl.king.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zyl.king.VO.OrderVO;
import com.zyl.king.mybatis.util.SnowflakeUtils;
import com.zyl.king.po.TOrder;
import com.zyl.king.mapper.OrderMapper;
import com.zyl.king.result.Result;
import com.zyl.king.result.ResultCode;
import com.zyl.king.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyl.king.utils.AssertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2025-01-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, TOrder> implements IOrderService {


    @Override
    public Result addOrder(OrderVO vo) {
        //校验
        try {
            AssertUtils.isNull(vo.getOrderAmount(),"订单金额不能为空");
            AssertUtils.isNull(vo.getRecipientName(),"收货人姓名不能为空");
            AssertUtils.isNull(vo.getRecipientPhone(),"收货人电话不能为空");
            AssertUtils.isNull(vo.getShippingAddress(),"收货地址不能为空");
        }catch (Exception e){
            return Result.error(e.getMessage(),ResultCode.PARAM_ERROR);
        }
        try {
            TOrder TOrder = new TOrder();
            BeanUtils.copyProperties(vo, TOrder);
            save(TOrder);
        }catch (Exception e){
            log.error("报错",e);
            return Result.error(ResultCode.SYSTEM_ERROR);
        }
        return Result.success();
    }

    @Override
    public Result queryOrder(String userName) {
        List<TOrder> list = list(new LambdaQueryWrapper<TOrder>()
                .eq(!ObjectUtils.isEmpty(userName), TOrder::getRecipientName, userName));
        return Result.success(list);
    }
}
